package cn.dsp.admin.controller;

import cn.dsp.admin.entity.admin.Role;
import cn.dsp.admin.entity.lot.LList;
import cn.dsp.admin.entity.lot.Lot;
import cn.dsp.admin.service.ILotService;
import cn.dsp.admin.service.ISpaceService;
import cn.dsp.admin.service.ex.ServiceException;
import cn.dsp.admin.utils.IteratorUtils;
import cn.dsp.admin.utils.JsonResult;
import cn.dsp.admin.vo.ListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("lot")
public class LotController extends BaseController{

    @Autowired
    private ILotService lotService;

    //根据商家id获得所属停车场信息
    @RequestMapping("get_lot_list")
    public JsonResult<List<ListVO>> getLotAll(HttpSession session){
        Integer storeId = getStoreIdFromSession(session);
        List<Lot> lots = lotService.getAllLot(storeId);
        lots = new IteratorUtils().LotIterator(lots,storeId);
        List<ListVO> data=lotService.getAllList(lots);
        return new JsonResult<>(SUCCESS,data);
    }

    //根据停车场id获得停车场VO信息
    @RequestMapping("get_lot_by/{lotId}")
    public JsonResult<ListVO> getLotByLotId(@PathVariable("lotId") Integer lotId){
        ListVO data=lotService.getLotByLotId(lotId);
        return new JsonResult<>(SUCCESS,data);
    }

    //根据停车场id删除停车场
    @RequestMapping("delete_lot_by/{lotId}")
    public JsonResult<Void> deleteById(@PathVariable("lotId") Integer lotId){
        lotService.deleteById(lotId);
        return new JsonResult<>(SUCCESS);
    }

    //根据关键字搜索停车场信息
    @RequestMapping("get_lot_like/{lotName}")
    public JsonResult<List<ListVO>> getByKey(@PathVariable("lotName") String lotName,HttpSession session){
        Integer storeId = getStoreIdFromSession(session);
        List<ListVO> data = lotService.getByKey(lotName,storeId);
        return new JsonResult<>(SUCCESS,data);
    }

    //添加新停车场信息
    @RequestMapping("add_new")
    public JsonResult<Void> addLot(Lot lot){
        try {
            lotService.addLot(lot);
        }catch (ServiceException e){
            return new JsonResult<>(e);
        }
        return new JsonResult<>(SUCCESS);
    }

    //更新停车场信息
    @RequestMapping("update_info")
    public JsonResult<Void> changeInfo(Lot lot){
        lotService.updateLot(lot);
        return new JsonResult<>(SUCCESS);
    }

    //根据停车场id查询所有停车位锁状态
    @RequestMapping("get_Locks_by/{lotId}")
    public JsonResult<List<String>> getByKey(@PathVariable("lotId") Integer lotId){
        List<String> data = lotService.getIsLocks(lotId);
        return new JsonResult<>(SUCCESS,data);
    }

    //根据停车场id和停车位id修改车位锁状态
    @RequestMapping("update_isLock")
    public JsonResult<Void> updateIsLock(Integer lotId,Integer spaceId){
        lotService.updateIsLock(lotId,spaceId);
        return new JsonResult<>(SUCCESS);
    }
}
